package com.example.reggie.common;

import com.example.reggie.entity.Orders;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.RichTextString;

import java.io.FileOutputStream;
import java.util.List;

public class ExportExcel {
    public void getValue(List<Orders> userList, FileOutputStream fout){
        try{
            //1.创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //标题样式
            HSSFCellStyle colStyle = createCellStyle(workbook,(short)10,true,true);
            //内容样式
            HSSFCellStyle cellStyle = createCellStyle(workbook,(short)10,false,true);
            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("订单明细");
            //设置默认列宽
            sheet.setDefaultColumnWidth(15);

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(0);
            String[] titles = {"id","订单号","订单状态","下单用户id","地址id",
                    "下单时间","结账时间","支付方式 (1微信，2支付宝)","实收金额","备注",
                    "手机号","地址","用户名","收货人"};//""为占位字符串
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }


            //4.操作单元格;将用户列表写入excel
            if(userList != null)
            {
                int i=1;
                for(int j=0;j<userList.size();j++)
                {
                    //创建数据行,前面有两行,头标题行和列标题行
                    HSSFRow row3 = sheet.createRow(j+1);
                    HSSFCell cell0 = row3.createCell(0);
                    cell0.setCellStyle(cellStyle);
                    cell0.setCellValue(i++);

                    HSSFCell cell1 = row3.createCell(1);
                    cell1.setCellStyle(cellStyle);
                    cell1.setCellValue(userList.get(j).getId());

                    HSSFCell cell2 = row3.createCell(2);
                    cell2.setCellStyle(cellStyle);
                    cell2.setCellValue(userList.get(j).getNumber());

                    HSSFCell cell3 = row3.createCell(3);
                    cell3.setCellStyle(cellStyle);
                    cell3.setCellValue(userList.get(j).getStatus());

                    HSSFCell cell4 = row3.createCell(4);
                    cell4.setCellStyle(cellStyle);
                    cell4.setCellValue(userList.get(j).getUserId());

                    HSSFCell cell5 = row3.createCell(5);
                    cell5.setCellStyle(cellStyle);
                    cell5.setCellValue(userList.get(j).getAddressBookId());

                    HSSFCell cell6 = row3.createCell(6);
                    cell6.setCellStyle(cellStyle);
                    cell6.setCellValue(userList.get(j).getOrderTime());

                    HSSFCell cell7= row3.createCell(7);
                    cell7.setCellStyle(cellStyle);
                    cell7.setCellValue(userList.get(j).getCheckoutTime());

                    HSSFCell cell8= row3.createCell(8);
                    cell8.setCellStyle(cellStyle);
                    cell8.setCellValue(userList.get(j).getPayMethod());


/*                    HSSFCell cell9= row3.createCell(9);
                    cell9.setCellStyle(cellStyle);
                    cell9.setCellValue((RichTextString) userList.get(j).getAmount());*/


                    HSSFCell cell10= row3.createCell(10);
                    cell10.setCellStyle(cellStyle);
                    cell10.setCellValue(userList.get(j).getRemark());

                    HSSFCell cell11= row3.createCell(11);
                    cell11.setCellStyle(cellStyle);
                    cell11.setCellValue(userList.get(j).getPhone());

                    HSSFCell cell12= row3.createCell(12);
                    cell12.setCellStyle(cellStyle);
                    cell12.setCellValue(userList.get(j).getAddress());

                    HSSFCell cell13= row3.createCell(13);
                    cell13.setCellStyle(cellStyle);
                    cell13.setCellValue(userList.get(j).getUserName());

                    HSSFCell cell14= row3.createCell(14);
                    cell14.setCellStyle(cellStyle);
                    cell14.setCellValue(userList.get(j).getConsignee());
                }
            }

            //5.输出
            workbook.write(fout);
//              workbook.close();
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param workbook
     * @param fontsize
     * @return 单元格样式
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize,boolean flag,boolean flag1) {
        // TODO Auto-generated method stub
        HSSFCellStyle style = workbook.createCellStyle();
        //是否水平居中
        /*if(flag1){
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        }*/

//        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        //是否加粗字体
        if(flag){
//            font.setBold(HSSFFont.BOLDWEIGHT_BOLD);
        }
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }
}
